Aggregations in R
How to use aggregates in R with Plotly.
Introduction
Aggregates are a type of transform that can be applied to values in a given expression. Available aggregations are:
| Function | Description |
|---|---|
count |
Returns the quantity of items for each group. |
sum |
Returns the summation of all numeric values. |
avg |
Returns the average of all numeric values. |
median |
Returns the median of all numeric values. |
mode |
Returns the mode of all numeric values. |
rms |
Returns the rms of all numeric values. |
stddev |
Returns the standard deviation of all numeric values. |
min |
Returns the minimum numeric value for each group. |
max |
Returns the maximum numeric value for each group. |
first |
Returns the first numeric value for each group. |
last |
Returns the last numeric value for each group. |
Basic Example
library(plotly)
p <- plot_ly(
type = 'scatter',
x = diamonds$cut,
y = diamonds$price,
mode = 'markers',
transforms = list(
list(
type = 'aggregate',
groups = diamonds$cut,
aggregations = list(
list(
target = 'y', func = 'sum', enabled = T
)
)
)
)
)
p
Aggregate Functions
library(plotly)
s <- schema()
agg <- s$transforms$aggregate$attributes$aggregations$items$aggregation$func$values
l = list()
for (i in 1:length(agg)) {
ll = list(method = "restyle",
args = list('transforms[0].aggregations[0].func', agg[i]),
label = agg[i])
l[[i]] = ll
}
p <- plot_ly(
type = 'scatter',
x = diamonds$cut,
y = diamonds$price,
mode = 'markers',
marker = list(
size = 10,
color = 'blue',
opacity = 0.8
),
transforms = list(
list(
type = 'aggregate',
groups = diamonds$cut,
aggregations = list(
list(
target = 'y', func = 'avg', enabled = T
)
)
)
)
) %>%
layout(
title = '<b>Plotly Aggregations</b><br>use dropdown to change aggregation',
xaxis = list(title = 'Cut'),
yaxis = list(title = 'Price ($)'),
updatemenus = list(
list(
x = 0.25,
y = 1.04,
xref = 'paper',
yref = 'paper',
yanchor = 'top',
buttons = l
)
)
)
p
Histogram Binning
library(plotly)
df <- read.csv("https://plot.ly/~public.health/17.csv")
labels <- function(size, label) {
list(
args = c("xbins.size", size),
label = label,
method = "restyle"
)
}
p <- df %>%
plot_ly(
x = ~date,
autobinx = FALSE,
autobiny = TRUE,
marker = list(color = "rgb(68, 68, 68)"),
name = "date",
type = "histogram",
xbins = list(
end = "2016-12-31 12:00",
size = "M1",
start = "1983-12-31 12:00"
)
) %>%
layout(
paper_bgcolor = "rgb(240, 240, 240)",
plot_bgcolor = "rgb(240, 240, 240)",
title = "<b>Shooting Incidents</b><br>use dropdown to change bin size",
xaxis = list(
type = 'date'
),
yaxis = list(
title = "Incidents"
),
updatemenus = list(
list(
x = 0.1,
y = 1.15,
active = 1,
showactive = TRUE,
buttons = list(
labels("D1", "Day"),
labels("M1", "Month"),
labels("M6", "Half Year"),
labels("M12", "Year")
)
)
)
)
p
Mapping with Aggregations
library(plotly)
df <- read.csv("https://raw.githubusercontent.com/bcdunbar/datasets/master/worldhappiness.csv")
s <- schema()
agg <- s$transforms$aggregate$attributes$aggregations$items$aggregation$func$values
l = list()
for (i in 1:length(agg)) {
ll = list(method = "restyle",
args = list('transforms[0].aggregations[0].func', agg[i]),
label = agg[i])
l[[i]] = ll
}
p <- df %>%
plot_ly(
type = 'choropleth',
locationmode = 'country names',
locations = ~Country,
z = ~HappinessScore,
autocolorscale = F,
reversescale = T,
colorscale = 'Portland',
transforms = list(list(
type = 'aggregate',
groups = ~Country,
aggregations = list(
list(target = 'z', func = 'sum', enabled = T)
)
))
) %>%
layout(
title = "<b>World Happiness</b>",
geo = list(
showframe = F,
showcoastlines = F
),
updatemenus = list(
list(
x = 0.25,
y = 1.04,
xref = 'paper',
yref = 'paper',
yanchor = 'top',
buttons = l
)
)
)
p
Reference
See https://plot.ly/r/reference/ for more information and options!

